//
//  ZCEmoticonKeyBoard.swift
//  weibo
//
//  Created by Ning Xie on 16/8/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

private let EmoticonCollectionViewIdentifier = "EmoticonCollectionViewIdentifier"

class ZCEmoticonKeyBoard: UIView {

    private lazy var emoticonToolBar: ZCEmoticonToolBar = {
       let toolbar = ZCEmoticonToolBar(frame: CGRectZero)
        
    
        return toolbar
    }()
    
    private lazy var emoticonCollectionView: UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .Horizontal
        
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        view.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: EmoticonCollectionViewIdentifier)
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.pagingEnabled = true
        view.bounces = false
        view.dataSource = self
        view.delegate = self
        view.registerClass(ZCEmoticonKeyBoardCell.self, forCellWithReuseIdentifier: EmoticonCollectionViewIdentifier)
        
        return view
        
    }()
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){

        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        addSubview(emoticonToolBar)
        addSubview(emoticonCollectionView)
        
        emoticonToolBar.snp_makeConstraints { (make) in
            make.bottom.trailing.leading.equalTo(self)
            make.height.equalTo(35)
        }
        emoticonCollectionView.snp_makeConstraints { (make) in
            make.top.equalTo(self)
            make.bottom.equalTo(emoticonToolBar.snp_top)
            make.leading.trailing.equalTo(self)
        }
        
        emoticonToolBar.didSelectedButtonClosure = {[weak self] (type:emoticonToolBarType)->() in
            
            let indexPath:NSIndexPath
            switch type {
            case .leftBtn:
//                print("默认")
                indexPath = NSIndexPath(forItem: 0, inSection: 0)
            case .middleBtn:
//                print("emoji")
                indexPath = NSIndexPath(forItem: 0, inSection: 1)
            case .rightBtn:
//                print("浪小花")
                indexPath = NSIndexPath(forItem: 0, inSection: 2)
            }
            self?.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition:.Left, animated: false)
        }
        
        
    }
    
   
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let flowLayout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        flowLayout.itemSize = emoticonCollectionView.size
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
    }
}

extension ZCEmoticonKeyBoard:UICollectionViewDataSource,UICollectionViewDelegate{
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return ZCEmoticonTools.sharedZCEmoticonTools.allEmoticon.count
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ZCEmoticonTools.sharedZCEmoticonTools.allEmoticon[section].count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(EmoticonCollectionViewIdentifier, forIndexPath: indexPath) as! ZCEmoticonKeyBoardCell
        
        cell.emoticonModel = ZCEmoticonTools.sharedZCEmoticonTools.allEmoticon[indexPath.section][indexPath.item]
        
        return cell
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let cells = emoticonCollectionView.visibleCells().sort { (firstCell, secondCell) -> Bool in
            return firstCell.x < secondCell.x
        }
        if cells.count == 2{
            
            let firstCell = cells.first!
            let secondCell = cells.last!
            let firstCellContentOfSetX = abs(firstCell.x - scrollView.contentOffset.x)
            let secondCellContentOfSetX = secondCell.x - scrollView.contentOffset.x
            
            let cell:UICollectionViewCell
            if firstCellContentOfSetX < secondCellContentOfSetX {
//                print("第一个cell显示的多")
                cell = firstCell
            }else{
//                print("第二个cell显示的多")
                cell = secondCell
            }
            
            let indexPath = emoticonCollectionView.indexPathForCell(cell)!
            
            let section = indexPath.section
            
            emoticonToolBar.selectedEmoticonButton(section)
        }
    }
    
    
}